import type { resData } from "@/dataType/data";
/**
 *
 * @param {string}url :上传图片地址
 * @param {string}ok_msg :成功成功消息
 * @param {string}err_msg :上传失败的消息
 * @returns Promise
 */
export const uploadImg = (
  url: string,
  ok_msg: string,
  err_msg: string
): Promise<resData> => {
  return new Promise((resolve, reject) => {
    //选择本地图片
    uni.chooseMedia({
      //同时上传几张,最多9张
      count: 1,
      //选择的类型:image图片,video视频
      mediaType: ["image"],
      //选择图片是否压缩 compressed:压缩
      sizeType: ["compressed"],
      success(res: any) {
        console.log("选择图片：", res);
        uni.showLoading({
          title: ok_msg,
        });
        //再调用uni.uploadfile实现上传
        uni.uploadFile({
          url,
          filePath: res.tempFiles[0].tempFilePath,
          name: "file",
          header: { accept: "application/json" },
          success(res_img) {
            resolve(res_img);
            uni.hideLoading();
          },
          fail(err) {
            uni.showToast({
              title: err_msg,
            });
            uni.hideLoading();
          },
        });
      },
    });
  });
};
